/**
 * Created by Administrator on 2016/3/28.
 * 构造日历
 */

function calendar(data,date){
    if(!date){
        date = new Date();
    }
    var $year = date.getFullYear();
    var $month = date.getMonth();
    var now = new Date();
    var time = new Date($year,$month,1);
    var last_time = new Date($year,$month+1,0);//月末
    var $str = '<table  class="calendar">';
    $str += "<tr>";
    $str += '<th>星期日</th>';
    $str += '<th>星期一</th>';
    $str += '<th>星期二</th>';
    $str += '<th>星期三</th>';
    $str += '<th>星期四</th>';
    $str += '<th>星期五</th>';
    $str += '<th>星期六</th>';
    $str += '</tr>';

    //这个月有多少天
    var $days=last_time.getDate();
    //第一天是星期几
    var $fstdw = time.getDay();
    //if($fstdw==0){$fstdw=7}
    //本月最后一天是星期几
    var $lstdw = last_time.getDay();
    if($lstdw==0){$lstdw=7}
    //要输出多少行
    var $rows = Math.ceil( ($days + $fstdw - 1) / 7 );
    if($lstdw == 7) $rows += 1;//如果本月最后一天是星期天，则行数加1
    var $cd = 0;
    var $cd2 = 0;
    /*数据拼装*/
    var $list_data = []; //月指标中的日指标
    var $key;
    for ( $key in data) {
        var day=new Date($key*1000);
        var $key_day = day.getDate();
        $list_data [$key_day] = data[$key];
    }
    //baison_app.api.alert({title:'cendl',msg:$list_data});
    //$list_data['29']={zbje:19999};
    /*完成拼装*/
    for(var $i = 0; $i < $rows; $i ++) {

        //日期
        $str += '<tr class="cc">';
        for(var $j = 0; $j < 7; $j ++) {
            $str += "<td style='border-bottom-style:none;text-align: right'>";
            if ($cd >= $fstdw && $cd < $days + $fstdw) {
                var $oday = $cd - $fstdw + 1;
                if ($oday == now.getDate())
                    $str += "<b><u>";
                if ($cd - $fstdw + 1) {
                    $str += ($cd - $fstdw + 1);
                }
                $str += "</b></u>";
            } else {
                $str += " ";
            }
            $cd ++;
            $str += "</td>";
        }
        $str += "</tr>";

        //指标
        $str += "<tr>";
        for(var $p = 0; $p < 7; $p ++) {

            $str += '<td style="border-top-style:none;border-bottom-style:none; color:#c69702;">';
            if ($cd2 >= $fstdw && $cd2 < $days + $fstdw) {
                $oday = $cd2 - $fstdw + 1;
                if (typeof ($list_data [$oday] )!='undefined' && $list_data [$oday]['zbje'] > 0) {
                    //					$str += $list_data[$oday];
                    $str += '<span>' + $list_data [$oday] ['zbje'] + '</span>';
                } else {
                    $str += '<span>0.00</span>';
                }
                $str += '<input type="hidden" name="jrzb_rq" value="' + $year + '-' + $month + '-' + $oday + '">';
            } else {
                $str += " ";
            }
            $cd2 ++;
            $str += "</td>";
        }
        $str += "</tr>";

        //操作按钮
        $str += "<tr>";
        for(var $op = 0; $op < 7; $op ++) {
            $str += "<td style='border-top-style:none'>";
            //$str += "<button ng-click='hello()'>set</button>";
            //$str += "<button>ok</button>";
            $str += "<button></button>";
            $str += "</td>";
        }
        $str += "</tr>";
    }
    $str += "</table>";
    return $str;
}
